package JWT;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.DatatypeConverter;

public class JWTTest {

    String apiKeySecret = "10001";
    Logger logger = LoggerFactory.getLogger(JWTTest.class);

    public String testCreateToken() {
        Identity identity = new Identity();
        identity.setId("123456");
        identity.setIssuer("fwpsl");
        identity.setDuration(1000 * 60 * 60 * 24l);
        identity.setUserName("fwpsl");
        identity.setRole("Manager");
        String token = JwtUtil.createToken(identity, apiKeySecret);

        System.out.println(token);
        return token;
    }

    public Identity testParseToken(String token) {
        // 验证token的有效性
        try {
            Identity identity = JwtUtil.parseToken(token, apiKeySecret);
            //把identity存入session中
            logger.info("用户：{}token有效", identity.getUserName());
            return identity;
        } catch (Exception e) {
            logger.info("token无效，转到登录界面");
            return null;
        }
    }

    public static void main(String[] args) {
        JWTTest jwt = new JWTTest();

        System.out.println(jwt.testParseToken(jwt.testCreateToken()));
    }
}
